package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.JobFieldProtectiveFacilities;
import com.ruoyi.system.domain.TemporaryStaffAccount;
import com.ruoyi.system.domain.ViewConfigPushMes;
import com.ruoyi.system.entity.SysRemind;
import com.ruoyi.system.mapper.JobFieldProtectiveFacilitiesMapper;
import com.ruoyi.system.mapper.ViewConfigPushMesMapper;
import com.ruoyi.system.service.JobFieldProtectiveFacilitiesService;
import com.ruoyi.system.service.SysRemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 作业场地防护设施(JobFieldProtectiveFacilities)表服务实现类
 *
 * @author makejava
 * @since 2022-11-15 15:08:36
 */
@Service
public class JobFieldProtectiveFacilitiesServiceImpl implements JobFieldProtectiveFacilitiesService {
    @Autowired
    private JobFieldProtectiveFacilitiesMapper jobFieldProtectiveFacilitiesDao;

    @Autowired
    private SysRemindService sysRemindService;
    @Resource
    private ViewConfigPushMesMapper viewConfigPushMesMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public JobFieldProtectiveFacilities queryById(Integer id) {
        return this.jobFieldProtectiveFacilitiesDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param jobFieldProtectiveFacilities 筛选条件
     * @param pageRequest                  分页对象
     * @return 查询结果
     */
    @Override
    public Page<JobFieldProtectiveFacilities> queryByPage(JobFieldProtectiveFacilities jobFieldProtectiveFacilities, PageRequest pageRequest) {
        long total = this.jobFieldProtectiveFacilitiesDao.count(jobFieldProtectiveFacilities);
        return new PageImpl<>(this.jobFieldProtectiveFacilitiesDao.queryAllByLimit(jobFieldProtectiveFacilities, pageRequest), pageRequest, total);
    }

    @Override
    public List<JobFieldProtectiveFacilities> selectList(JobFieldProtectiveFacilities jobFieldProtectiveFacilities) {
        return jobFieldProtectiveFacilitiesDao.queryAll(jobFieldProtectiveFacilities);
    }

    /**
     * 新增数据
     *
     * @param jobFieldProtectiveFacilities 实例对象
     * @return 实例对象
     */
    @Override
    public JobFieldProtectiveFacilities insert(JobFieldProtectiveFacilities jobFieldProtectiveFacilities) {
        this.jobFieldProtectiveFacilitiesDao.insert(jobFieldProtectiveFacilities);
        return jobFieldProtectiveFacilities;
    }

    /**
     * 修改数据
     *
     * @param jobFieldProtectiveFacilities 实例对象
     * @return 实例对象
     */
    @Override
    public JobFieldProtectiveFacilities update(JobFieldProtectiveFacilities jobFieldProtectiveFacilities) {
        this.jobFieldProtectiveFacilitiesDao.update(jobFieldProtectiveFacilities);
        return this.queryById(jobFieldProtectiveFacilities.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.jobFieldProtectiveFacilitiesDao.deleteById(id) > 0;
    }

    @Override
    public String importExcel(List<JobFieldProtectiveFacilities> jobFieldProtectiveFacilitiess, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(jobFieldProtectiveFacilitiess) || jobFieldProtectiveFacilitiess.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        jobFieldProtectiveFacilitiesDao.insertBatch(jobFieldProtectiveFacilitiess);
        return "成功";
    }

    @Scheduled(cron = "0 0 6 * * ?")
    public void checkPermitReportFile() {
        JobFieldProtectiveFacilities jobFieldProtectiveFacilities = new JobFieldProtectiveFacilities();
        List<JobFieldProtectiveFacilities> relateds = jobFieldProtectiveFacilitiesDao.queryAll(jobFieldProtectiveFacilities);
        if (CollectionUtils.isEmpty(relateds)) {
            return;
        }

        relateds.forEach(var -> {
            String remind = var.getNextCheckTime();
            Date date = new Date();
            boolean after = date.after(DateUtils.parseDate(remind));
            if (after) {
                String stringBuffer =
                        "安全设施名称:" + var.getSecurityFacilitiesName() +
                                "即将到期。";

                ViewConfigPushMes viewConfigPushMes = new ViewConfigPushMes();
                viewConfigPushMes.setViewName("作业场所防护设施");
                List<ViewConfigPushMes> configPushMes = viewConfigPushMesMapper.queryByViewKey(viewConfigPushMes);
                if (!CollectionUtils.isEmpty(configPushMes)) {
                    ViewConfigPushMes pushMes = configPushMes.stream().findFirst().orElse(new ViewConfigPushMes());
                    if (com.ruoyi.common.utils.StringUtils.isNotEmpty(pushMes.getViewValue())) {
                        boolean contains = pushMes.getViewValue().contains(",");
                        if (contains) {
                            String[] split = pushMes.getViewValue().split(",");
                            for (String vars : split) {
                                sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, Integer.valueOf(vars));
                            }
                        } else {
                            sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, Integer.valueOf(pushMes.getViewValue()));
                        }
                    }
                }
            }
        });
    }

}
